﻿using System;
using System.Linq;

namespace Middleware
{
    [Serializable]
    public class CalculatorImpl : RemoteObject, ICalculator
    {
        public CalculatorImpl() { }

        public CalculatorImpl(bool usePooling)
            : base(usePooling) { }

        #region ICalculatorService Members

        public int Sum(params int[] values) {
            return values.Sum();
        }

        public int Sub(params int[] values) {
            if (values.Length == 0) return 0;
            if (values.Length == 1) return values[0];
            int result = values[0];
            for (int i = 1; i < values.Length; i++) {
                int valueb = values[i];
                result -= valueb;
            }
            return result;
        }

        public int Mul(params int[] values) {
            return values.Aggregate(1, (current, item) => current * item);
        }

        public int Div(params int[] values) {
            if (values.Length == 0) return 0;
            if (values.Length == 1) return values[0];
            int value = values[0];
            for (int i = 1; i < values.Length; i++) {
                value /= values[i];
            }
            return value;
        }

        #endregion
    }
}
